<?php

namespace app\admin\model\teacher;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Teacher extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'view_teacher';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'addtime_text',
        'updatedtime_text'
    ];

    public $id =0;
    public $xing_name = '';
    public $active_zong = '';
    public $active_qu = '';
    public $push = 0;
    public $card_id = '';
    public $bonus = '';
    public $weigh = '';
    public $addtime = '';
    public $deletetime = '';
    public $updatedtime = '';

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    



    public function getAddtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['addtime']) ? $data['addtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdatedtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatedtime']) ? $data['updatedtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAddtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdatedtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function card()
    {
        return $this->belongsTo('app\admin\model\view\Card', 'card_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shengji()
    {
        if ($this->id > 0){
            $teacher = $this->find($this->id);
            $new_teacher = Db::table('fa_view_teacher')
                ->where('id','<>',$this->id)
                ->where('push','<=',$this->push)
                ->where('active_zong','<=',$this->active_zong)
                ->where('active_qu','<=',$this->active_qu)
                ->find();
        }else{
            $new_teacher = Db::table('fa_view_teacher')
                ->where('push','<=',$this->push)
                ->where('active_zong','<=',$this->active_zong)
                ->where('active_qu','<=',$this->active_qu)
                ->find();
        }

        return $new_teacher;

    }
}
